package com.changan.controller;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changan.entity.User;
import com.changan.enums.UserTypeEnum;
import com.changan.exception.BusinessException;
import com.changan.response.Result;
import com.changan.response.ResultCode;
import com.changan.service.LoginLogService;
import com.changan.service.UserService;
import com.changan.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author NieChangan
 * @since 2022-08-30
 */
@Api(value = "系统用户模块", tags = "系统用户接口")
//@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginLogService loginLogService;


    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @GetMapping("/getUserList")
    Result getUsers(){
        List<User> users = userService.list();

        return Result.ok().data("users",users);
    }


    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @GetMapping("/getUserById")
    Result getUserById(Integer id){
        User user = userService.getById(id);
        if(user==null){
            //这里虽然抛出了异常,但是没有被springboot捕获
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return Result.ok().data("user",user);
    }

    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @PostMapping("/findUserList")
    public Result findUserList(@RequestParam(value = "current",required = true,defaultValue = "1")Integer current,
                               @RequestParam(value = "size",required = true,defaultValue = "7")Integer size,
                               @RequestBody UserVO userVO){
        Page<User> page = new Page<>(current,size);
        Page<User> userPage = userService.getUserByPage(page,userVO);
        if(current>page.getPages()){
            page= new Page<>(page.getPages(),size);
            userPage = userService.getUserByPage(page,userVO);
        }
        return Result.ok().data("userPage",userPage);
    }

    @ApiOperation(value = "添加用户", notes = "添加用户信息")
    @PostMapping("/add")
    public Result add(@RequestBody UserVO userVO){
        User user = new User();
        //user.setType(UserTypeEnum.SYSTEM_USER);
        return Result.ok();
    }

    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response, @RequestBody UserVO userVO) throws IOException {
        userService.exportExcel(response,userVO);
    }

    /**
     * 用户登入
     *
     * @param username: 用户名
     * @param password: 密码
     * @return
     */
    @ApiOperation(value = "用户登入", notes = "接收参数用户名和密码,登入成功后,返回JWTToken")
    @PostMapping("/login")
    public Result login(@RequestParam(value = "username",required = true) String username,
                        @RequestParam(value = "password",required = true) String password,
                        HttpServletRequest request) {
        String token = userService.login(username, password);
        loginLogService.add(request);
        return Result.ok().data("token",token);
    }
}
